public class MyList implements IList{


    public class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    ListNode head;
    ListNode last;

    @Override
    public void addFirst(int data) {

        ListNode p = new ListNode(data);

        if (head == null) {
            head = p;
            last = p;
        } else {
            p.next = head;
            head.prev = p;
            head = p;
        }
    }

    @Override
    public void addLast(int data) {

        ListNode p = new ListNode(data);
        if (head == null) {
            head = p;
            last = p;
        } else {
            p.prev = last;
            last.next = p;
            last = p;
        }
    }

    @Override
    public void addIndex(int index, int data) {

        if (index < 0 || index > size()) {
            return;
        }
        ListNode p = new ListNode(data);

        if(index == 0) {
            p.next = head;
            head.prev = p;
            head = p;
            return;
        }
        if (index == size()) {
            last.next = p;
            p.prev = last;
            last = p;
            return;

        }
        ListNode t = head;
        int c = index;
        while (c != 0) {
            t = t.next;
            c--;
        }
        p.next = t;
        p.prev = t.prev;
        t.prev.next = p;
        t.prev = p;
    }

    @Override
    public boolean contains(int key) {

        ListNode p = head;
        while (p != null) {
            if (p.val == key) {
                return true;
            }
            p = p.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {

        if (head == null) {
            return;
        }
        ListNode p = head;
        if (head.val == key) {
            head = head.next;
            if (head.next != null) {
                head.prev = null;
            }
            return;
        }
        while (p.val != key) {
            p = p.next;
            if (p == null) {
                return;
            }
        }

        p.prev.next = p.next;
        if (p.next != null) {
            p.next.prev = p.prev;
        }
    }

    @Override
    public void removeAllKey(int key) {

        if (head == null) {
            return;
        }
        ListNode p = head;
        while (p != null) {

            if (p.val == key) {
                if (p == head) {

                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }
                } else {

                    p.prev.next = p.next;
                    if (p.next != null) {
                        p.next.prev = p.prev;
                    } else {
                        last = last.prev;
                    }
                }

                p = p.next;
            }
        }
    }

    @Override
    public int size() {
        int counst = 0;
        ListNode p = head;
        while (p != null) {
            counst++;
            p = p.next;
        }
        return counst;
    }

    @Override
    public void clear() {

        if (head == null) {
            return;
        }
        ListNode p = head;
        while (p != null) {
            ListNode t = p.next;
            p.prev = null;
            p.next = null;
            p = t;
        }
        head = last = null;
    }

    @Override
    public void display() {

        if (head == null) {
            return;
        }
        ListNode p = head;
        while (p != null) {
            System.out.print(p.val + " ");
            p = p.next;
        }
        System.out.println();
    }
}
